package com.fs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fs.dao.ProductDao;
import com.fs.entity.Product;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;





@WebServlet("/product.do")
public class ProductServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
		
		}
	@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			/*HttpSession session=req.getSession();*/
			PrintWriter out=resp.getWriter();
			String type=req.getParameter("type");//琛ㄧず鎿嶄綔绫诲瀷	
			ProductDao prodao=new ProductDao();
			HttpSession session=req.getSession();
			/*Object user_id=(Object) session.getAttribute("user_id"); //娓犻亾鐧诲綍鏃剁殑applicant_id
*/			int productid;
			if("add".equals(type)){//娣诲姞绠�巻
				Product proinfo=requestGetData(req);
				int product_id=prodao.add(proinfo);
				productid=product_id;
				System.out.println("sa......"+product_id);
				session.setAttribute("product_id", product_id);//鎶婁骇鍝佷俊鎭瓨鍦�seesion
				//瀛樺熀鏈俊鎭璞℃斁鍦╮eq瀵硅薄涓�
				req.setAttribute("product_id", product_id);
				/*req.setAttribute("proinfo", proinfo);*/
				/*resp.sendRedirect("product.do?type=query");*/
                List<Product> prolist=prodao.getProductListAll();
				
				Product proinfo2=prodao.queryProductInfo(product_id);
				
				req.setAttribute("proinfo",proinfo2);
				req.setAttribute("prolist",prolist );
			 resp.sendRedirect("product.do?type=query");

				/*req.getRequestDispatcher("personal/index.jsp").forward(req, resp);*/
			}else if("query".equals(type)){
				int product_id=0;
				 
				if(session.getAttribute("product_id")!=null){
					product_id=(Integer)session.getAttribute("product_id");
				}
				System.out.println(product_id+"query");
				
				/*List<Product> prolist=prodao.getProductList(product_id);*/
				List<Product> prolist=prodao.getProductListAll();
				
				/*Product proinfo=prodao.queryProductInfo(product_id);*/
				
				/*req.setAttribute("proinfo",proinfo );*/
				req.setAttribute("prolist",prolist );
				req.getRequestDispatcher("personal/index.jsp").forward(req, resp);
			}else if("update".equals(type)){
				int product_id=0;
				
				if(session.getAttribute("product_id")!=null){
					
					product_id=(Integer)session.getAttribute("product_id");
				}
				
				List<Product> prolist=prodao.getProductList(product_id); 
				
				req.setAttribute("prolist", prolist);
				resp.sendRedirect("product.do?type=query");
				/*req.getRequestDispatcher("personal/index.jsp").forward(req, resp);*/

			}
			else if("modify".equals(type)){
				Product proinfo=getRequestModifyData(req);
				prodao.update(proinfo);
				resp.sendRedirect("product.do?type=query");
				
			}else if("delete".equals(type)){
				
				String product_id=req.getParameter("product_id");
				List<Product> prolist=prodao.delete((Integer.parseInt(product_id)));
				req.setAttribute("prolist",prolist );
				req.getRequestDispatcher("personal/index.jsp").forward(req, resp);
				System.out.println("dfad"+product_id);
			
			}				
		}
			

	private Product getRequestModifyData(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String product_id=req.getParameter("product_id");
		
		String product_name=req.getParameter("product_name");
		System.out.println(req.getParameter("product_name"));
		
		String product_price=req.getParameter("product_price");
		
		String product_describe=req.getParameter("product_describe");
		
		String product_cart=req.getParameter("product_cart"); 	
		Product productinfo=new Product(product_name,product_describe,product_price,product_cart);
		productinfo.setProduct_id(Integer.parseInt(product_id));
		return productinfo;
	
	}
	
	private Product requestGetData(HttpServletRequest req) {
		// TODO Auto-generated method stub
		String product_name=req.getParameter("product_name");
		System.out.println(req.getParameter("product_name"));
		String product_describe=req.getParameter("product_describe");
		
		String product_price=req.getParameter("product_price"); 
		
		Product productinfo=new Product(product_name,product_describe,product_price,null);

		return productinfo;
		
	
	}
}
